package it.softphone.cmc.social;

import java.awt.Desktop;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;
import org.scribe.builder.Google2Api;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;


public class GoogleExample2
{
	private static final String PROTECTED_RESOURCE_URL = "https://docs.google.com/feeds/default/private/full/";
	private static final String SCOPE = "https://docs.google.com/feeds/";

	OAuthService getService() {
		final OAuthService service = new ServiceBuilder()
				.provider(Google2Api.class)
				.apiKey("345704514741.apps.googleusercontent.com")
				.apiSecret("ViyxyNNaCzcebcPaRP7XZH-D")
				.scope(SCOPE)
				.callback("http://localhost:8080/result")
				.build();
		return service;

	}

    protected final Server server;

    protected static final int port = 8080;

    protected static final String BASEURI = "http://localhost:" + port + "/";

    public GoogleExample2() throws Exception {
    	
    	// SERVER NEEDS FOR GET ASYNC  NOTIFICATION
        server = new Server(port);
        //Connector connector = new SelectChannelConnector();
        //connector.setPort(port);
        //server.setConnectors(new Connector[]{connector});
        //server.setHandler(new WebAppContext("src/test/webapp", "/"));
        server.start();
        
    }
	
	public static void main(String[] args) throws Exception {
		final GoogleExample2 example = new GoogleExample2();
		
		final OAuthService service = example.getService();

		System.out.println(service.getAuthorizationUrl(null));

		if( Desktop.isDesktopSupported() ) {
			Desktop.getDesktop().browse( new java.net.URI(service.getAuthorizationUrl(null)));
		}

	
		final Handler h = new AbstractHandler() {

			@Override
			public void handle(String target, HttpServletRequest req, HttpServletResponse res, int dispatch) throws IOException, ServletException {

					java.io.PrintWriter w = res.getWriter();
					
					w.println("And paste the verifier here");
					w.print(">>");
					Verifier verifier = new Verifier(req.getParameter("code"));
					w.println();
			
					final OAuthService service = example.getService();

					
					// Trade the Request Token and Verfier for the Access Token
					w.println("Trading the Request Token for an Access Token...");
					
					Token accessToken = service.getAccessToken(null, verifier);
					
					w.println("Got the Access Token!");
					w.println("(if your curious it looks like this: "	+ accessToken + " )");
					w.println();
			
					// Now let's go and ask for a protected resource!
					w.println("Now we're going to access a protected resource...");
					OAuthRequest request = new OAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL);
					
					service.signRequest(accessToken, request);
					
					request.addHeader("GData-Version", "3.0");
					Response response = request.send();
					
					w.println("Got it! Lets see what we found...");
					w.println("<pre>");
					w.println(response.getCode());
					w.println("</pre></br>");
					w.println("<pre>");
					w.println(response.getBody());
					w.println("</pre>");
			
					w.println();
					w.println("Thats it man! Go and build something awesome with Scribe! :)");
					
				}
     	
        };
        
        example.server.setHandler( h );

		

	}
}